package com.wasu.doicommon.util;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.text.TextUtils;
import android.util.Log;

import com.wasu.UILApplication;
import com.wasu.doicommon.constant.AppConstant;

import java.util.Set;

/**
 * <p>------------------------------------------------------
 * <p>Copyright (C) 2018 wasu company, All rights reserved.
 * <p>------------------------------------------------------
 * <p>  AppCheckUtils apk升级检测工具(是否安装、版本号、插件apk检测)
 * <p>
 *
 * @author Created by zhangdaofeng
 * @date 2018/9/29
 */
public class AppCheckUtils {

    /**
     * 获取本地软件版本号名称
     */
    public static String getLocalVersionName(Context context) {
        String localVersion = "";
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getApplicationContext().getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (packageInfo != null) {
            localVersion = packageInfo.versionName;
        }
        return localVersion;
    }



    /***
     * 获得指定包名的 Intent
     *
     * @param context
     * @param packageName
     * @return
     */
    public static Intent getAppIntent(Context context, String packageName) {
        return context.getPackageManager().getLaunchIntentForPackage(packageName);
    }

    /***
     * 判断指定包名的 app 是否安装了。
     *
     * @param context
     * @param packageName
     * @return
     */
    public static boolean isAppInstall(Context context, String packageName) {
        return getAppIntent(context, packageName) != null;
    }



    /**
     * 检查华数蓝牙遥控器是否连接
     *
     * @return
     */
    public static boolean checkVoiceIsConnect() {
        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter == null) {
            return false;
        }
        Set<BluetoothDevice> devices = adapter.getBondedDevices();
        if (devices == null || devices.size() == 0) {
            return false;
        }
        for (BluetoothDevice device : devices) {
            if (device == null) {
                continue;
            }
            String name = device.getName();
            if (isWasuDevice(name)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 以下是华数语音遥控器链接的配置信息
     */
    private static final String NAME = "BV12";
    private static final String PRE_NAME = "DZ";
    private static final String Last_NAME = "BV";
    private static final String Last_NAME_0 = "B";

    /**
     * 是否匹配到华数语音遥控器的名字
     *
     * @param deviceName
     * @return
     */
    private static boolean isWasuDevice(String deviceName) {
        if (TextUtils.isEmpty(deviceName)) {
            return false;
        }
        // 去除空格 制表符等等
        String tmp = deviceName.replaceAll("\\s*", "");
        if (tmp.startsWith(PRE_NAME) && (tmp.endsWith(Last_NAME) || tmp.endsWith(Last_NAME_0))) {
            return true;
        }
        if (tmp.contains(NAME)) {
            return true;
        }
        return false;
    }

    /**
     * 保存已经获取的机顶盒类型
     */
    private static String TV_TYPE = null;

    /**
     * 提供外部调用
     *
     * @return
     */
    public static boolean isIpTVType() {
        return isIpTVType(UILApplication.getInstance().getApplication());
    }

    public static boolean isIpTVType(Context context) {
        Log.v("AppCheckUtils", "TV_TYPE: " + TV_TYPE);
        if (TextUtils.isEmpty(TV_TYPE)) {
            TV_TYPE = tvTypeDVBorIp(context);
        }
        return Boolean.parseBoolean(TV_TYPE);
    }

    public static void setTvType(String tvType) {
        TV_TYPE = tvType;
    }

    private static String tvTypeDVBorIp(Context context) {
        try {
            SharedPreferences sp = context.getSharedPreferences(AppConstant.TV_TYPE_SET_FILENAME, Context.MODE_MULTI_PROCESS);
            String type = sp.getString(AppConstant.TV_TYPE_SET_KEYNAME, AppConstant.TV_TYPE_DVB);
            Log.v("AppCheckUtils", "tvTypeDVBorIp; type: " + type);
            return String.valueOf(AppConstant.TV_TYPE_IP.equals(type));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "false";
    }

}
